#include "PrecompiledHeader.h"
#include "MeshInstance.h"

/*--------------------------------------------------------------------------*/

MeshInstance::MeshInstance(const NodeCreation& nc, const Mesh* mesh) :
	IRenderableObject(nc),
	m_mesh(mesh)
{
	RenderTaskDesc desc;
	desc.m_material = NULL;
	desc.m_argument = 0;
	desc.m_flags = 0;
	desc.m_priority = 0;
	desc.m_object = this;

	for (uint i = 0; i < this->m_mesh->GetSetsCount(); ++i)
	{
		desc.m_material = this->m_mesh->GetSets()[i].m_material;
		this->AddRenderTask(desc);
	}
}

/*--------------------------------------------------------------------------*/

MeshInstance::~MeshInstance()
{

}

/*--------------------------------------------------------------------------*/

void MeshInstance::RenderableBinder(uint treeCalcFlags)
{
#ifdef VL_RENDER_STATISTICS
	g_Hud->GetStatisticsManager()->IncrementStatistic("MeshInstance");
#endif

	for (uint i = 0; i < this->m_mesh->GetSetsCount(); ++i)
	{
		this->BindRenderTask(this->GetSceneInstance()->GetRenderPhase(), i + 10);
	}
}

/*--------------------------------------------------------------------------*/

void MeshInstance::RenderableHelper(const uint i, ERenderQuality::Type quality, uint pendingInstances)
{
	uint selSet = i - 10;

	if (selSet < this->m_mesh->GetSetsCount())
	{
		ROpenGL::BindVertexArray(this->m_mesh->GetSets()[selSet].m_vao);
		ROpenGL::DrawVertexArray(pendingInstances);
	}
}

/*--------------------------------------------------------------------------*/

void MeshInstance::RenderBoundingBox()
{
	ROpenGL::DepthMask(GL_FALSE);
	ROpenGL::ColorMask(glm::ivec4(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE));

	ROpenGL::BindVertexArray(this->m_mesh->GetBoundingBox());
	ROpenGL::DrawVertexArray();
}

/*--------------------------------------------------------------------------*/